﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Provider;
using CE.TravelInsurance;
using CE.Global;

public partial class Provider_TravelInsurance_ProviderTIAge : System.Web.UI.Page
{
    string PageName = "CXTI";
    string SubPageName = "CXTIAGELG";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!Page.IsPostBack)
        {
            ClearFields();

            if (Page.Request.Params["COV"] != null && Page.Request.Params["TID"] != null ) 
            {
                string _cover ="";
                string _tid = "";

                    _cover = Page.Request.Params["COV"].ToString();
                    _tid = Page.Request.Params["TID"].ToString();

                    //ddlLevelOfCoverage.Text = _level;
                    ddlPolicyType.Text = _cover;
                    lblTAId.Text = _tid;

                    this.CheckAgeEligibility(this, null);
                    //LoadAgeEligibility(_tid, _cover);
                    
                
            }
        }

    }


    //protected void LoadAgeEligibility(long TAID, string PolicyType)
    //{
    //    TAAge _TAAge = new TAAge();
    //    TAAgeDB _TAAgedb = new TAAgeDB();

    //    _TAAge = _TAAgedb.GetAgeEligibilityDTItem(TAID, PolicyType);

    //    if (_TAAge != null)
    //    {
    //        lblTAAgeId.Text = _TAAge.TAAgeId.ToString();
    //        ddlPolicyType.Text = PolicyType;
    //        lblPlanName.Text = _TAAge.PlanName;
    //        txtAdultAgeFrom.Text = _TAAge.AdultAgeFrom.ToString();
    //        txtAdultAgeTo.Text = _TAAge.AdultAgeTo.ToString();
    //        txtChildAgeFrom.Text = _TAAge.ChildAgeFrom.ToString();
    //        txtChildAgeTo.Text = _TAAge.ChildAgeTo.ToString();
    //    }


    //}


    protected void CheckAgeEligibility(object sender, EventArgs e)
    {
        ClearFields();

        TAQuoteResult _ta = new TAQuoteResult();
        TAQuoteResultDB _tadb = new TAQuoteResultDB();

       // _ta = _tadb.GetTASelectSingleItemSelected(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), ddlPolicyType.SelectedValue, ddlLevelOfCoverage.SelectedValue);
        _ta = _tadb.GetTAQuoteResult(Convert.ToInt64(lblTAId.Text), ddlPolicyType.SelectedValue);

        if (_ta != null)
        {
            lblTAId.Text = _ta.TAInsId.ToString();
            lblPlanName.Text = _ta.PlanName;

            TAAge _TAAge = new TAAge();
            TAAgeDB _TAAgedb = new TAAgeDB();

            //_TAAge = _TAAgedb.GetAgeEligibilityDTItem(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), ddlPolicyType.SelectedValue, ddlLevelOfCoverage.SelectedValue);
            _TAAge = _TAAgedb.GetAgeEligibilityDTItem(Convert.ToInt64(lblTAId.Text.ToString()), ddlPolicyType.SelectedValue);

            if (_TAAge != null)
            {
                lblTAId.Text = _TAAge.TAInsId.ToString();
                lblTAAgeId.Text = _TAAge.TAAgeId.ToString();
                //lblPlanName.Text = _TAAge.PlanName;
                txtAdultAgeFrom.Text = _TAAge.AdultAgeFrom.ToString();
                txtAdultAgeTo.Text = _TAAge.AdultAgeTo.ToString();
                txtChildAgeFrom.Text = _TAAge.ChildAgeFrom.ToString();
                txtChildAgeTo.Text = _TAAge.ChildAgeTo.ToString();
            }
        }


        


    }
    protected void ClearFields()
    {
        txtAdultAgeFrom.Text = "";
        txtAdultAgeTo.Text = "";
        txtChildAgeFrom.Text = "";
        txtChildAgeTo.Text = "";
        lblPlanName.Text = "";
        lblTAAgeId.Text = "0";

        lblMessage.Text = "";

    }

    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 600))
            {
                if (lblPlanName.Text != "")
                {
                    TAAge _TAAge = new TAAge();
                    TAAgeDB _TAAgedb = new TAAgeDB();

                    _TAAge.TAAgeId = Convert.ToInt64(lblTAAgeId.Text.Trim());
                    int _trantype = 1;
                    if (_TAAge.TAAgeId > 0)
                    {
                        _trantype = 2;
                    }
                    _TAAge.TAInsId = Convert.ToInt64(lblTAId.Text.Trim());
                    _TAAge.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                   // _TAAge.CoverageLevel = ddlLevelOfCoverage.SelectedValue;
                    _TAAge.AdultAgeFrom = Convert.ToInt32(txtAdultAgeFrom.Text.Trim());
                    _TAAge.AdultAgeTo = Convert.ToInt32(txtAdultAgeTo.Text.Trim());
                    _TAAge.ChildAgeFrom = Convert.ToInt32(txtChildAgeFrom.Text.Trim());
                    _TAAge.ChildAgeTo = Convert.ToInt32(txtChildAgeTo.Text.Trim());
                    _TAAge.PlanType = ddlPolicyType.SelectedValue.ToString();
                    _TAAge.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                    _TAAge.TAAgeId = _TAAgedb.Insert(_TAAge);

                    if (_TAAge.TAAgeId > 0)
                    {
                        InsertProviderAuthTransaction(_TAAge.TAAgeId, "TIAge", _trantype, 600, _authcode, _TAAge.ProviderCompanyId, _TAAge.RefCEUserId);
                        lblMessage.Text = "Saved successfully!";
                    }
                    else
                    {
                        lblMessage.Text = "Saving failed!";
                    }
                }
                else
                    lblMessage.Text = "Pls. create a plan under this Policy/Coverage level.";

            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }

        }
    }


    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderTIAge.aspx");
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            //btnNew.Enabled = false;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = true;
        }
    }

}
